import { $x } from 'party_fe_component'
import StorageUtil from 'party_fe_component/src/assets/js/utils/storageUtil'

export const Terminals = {
    PC: 0,
    APP: 1,
    Wechat: 2
}

var sessionUtil = new StorageUtil(sessionStorage)

export default {
    data() {
        return {
            fgDatas: {},
            fgData: {},
        }
    },
    methods: {
        loadSceneConfigs(pageName, tokenUtil = null) {
            var { moduleId, appId, businessType, spId, token } = tokenUtil || $x.tokenUtil
            var key = 'textConfig_' + [businessType, appId, moduleId, spId].join('_')

            var cached = sessionUtil.getObj(key)
            if (cached) {
                this.fgDatas = cached
                this.changeScenePage(pageName)
            } else {
                var pars = {
                    modelId: moduleId,
                    terminal: Terminals.PC,
                    appId,
                    businessType,
                    spId
                }
                var byHeaders = { appId, token, businessType }

                $x.get('{abos}/api/v61/doc/custom/scene', pars, { byHeaders }).then(response => {
                    const textConfig = (response.data && response.data.pages) || {}
                    var values = {}
                    Object.keys(textConfig).map(page => {
                        var map = {}
                        var locales = textConfig[page].locales || {}
                        Object.keys(locales).map(name => {
                            map[name] = locales[name]
                        })
                        values[page] = map
                    })

                    sessionUtil.setObj(key, values)
                    this.fgDatas = values
                    this.changeScenePage(pageName)
                })
            }
        },
        changeScenePage(pageName) {
            this.fgData = this.getSceneConfig(pageName)
        },
        getSceneConfig(pageName) {
            return (this.fgDatas && this.fgDatas[pageName]) || {}
        },
        getSceneText(pageName, name) {
            var data = this.getSceneConfig(pageName)
            var item = data[name] || {}
            return item.name
        },
        _mapSceneConfig(property) {
            var map = {}
            Object.keys(this.fgData).map(key => {
                map[key] = this.fgData[key][property]
            })
            return map
        }
    },
    computed: {
        fgTexts() {
            return this._mapSceneConfig('name')
        },
        fgHiddens() {
            return this._mapSceneConfig('desabled')
        }
    }
}
